package com.henu.engineer.service

import com.henu.engineer.dao.entity.user.Student
import com.henu.engineer.dao.entity.user.User
import com.henu.engineer.dao.mapper.UserXmlMapper
import com.henu.engineer.dao.mapper.student.StudentXmlMapper
import com.henu.engineer.dao.mapper.teacher.TeacherXmlMapper
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @author noexcept
 * @date 2021/7/26 6:34
 */
@Service
class UserService {
    @Autowired
    lateinit var userXmlMapper: UserXmlMapper

    @Autowired
    lateinit var studentXmlMapper: StudentXmlMapper

    @Autowired
    lateinit var teacherXmlMapper: TeacherXmlMapper

    @Autowired
    lateinit var loginService: LoginService

    fun insertUser(user: User) = userXmlMapper.insertUser(user)

    /**
     * 根据学号查找学生
     */
    fun getStudentInfo(id: String = loginService.currentUserId!!): Student? {
        return studentXmlMapper.selectStudent(id)
    }

    /**
     * 查询老师信息
     */
    fun getTeacherInfo(id: String = loginService.currentUserId!!) =
        teacherXmlMapper.selectTeacher(id)

    /**
     * 更新密码
     *
     * @param id
     * @param oldPwd 旧密码
     * @param newPwd 新密码,大于等于6，小于等于16
     * @return 是否成功
     */
    fun updatePwd(id: String, oldPwd: String, newPwd: String): String {
        if (oldPwd != userXmlMapper.selectPwd(id)) {
            return "OLD_PWD_ERROR"
        }
        return if (userXmlMapper.updatePwd(id, newPwd)) "SUCCESS" else "FAILED"
    }

    /**
     * 更新联系方式
     */
    fun updateContact(newContact: String, rank: String): Boolean {
        return if (STUDENT == rank)
            studentXmlMapper.updateContact(loginService.currentUserId!!, newContact)
        else teacherXmlMapper.updateContact(loginService.currentUserId!!, newContact)
    }

    /**
     * 更新老师身份证号
     */
    fun updateTeacherIdentity(identity: String) =
        teacherXmlMapper.updateIdentity(loginService.currentUserId!!, identity)

    /**
     * 修改特定项的信息 item只能是：name,gender,specialization,contact,identity之一，
     * 并符合相应的数据约束
     */
    fun updateStudentInfo(studentId: String, item: String, newValue: String): Boolean {
        when (item) {
            "gender" -> {
                if (newValue.length != 1)
                    return false
            }
            "identity" -> {
                if (newValue.length != 18)
                    return false
            }
            "name", "specialization", "contact" -> {
            }
            else -> return false
        }
        return studentXmlMapper.updateItemValue(studentId, item, newValue)
    }


    companion object {
        val logger: Logger = LoggerFactory.getLogger("StudentService")

        /**
         * 学生
         */
        const val STUDENT = "ST"

        /**
         * 普通教师
         */
        const val TEACHER = "TC"

        /**
         * 课程组负责人
         */
        const val COURSE_LEADER = "CL"

        /**
         * 专业负责人
         */
        const val PROFESSIONAL_LEADER = "PL"

        /**
         * 同时为专业负责人和课程组负责人
         */
        const val COURSE_PROFESSIONAL_LEADER = "CP"

        /**
         * 院级负责人
         */
        const val DEPART_LEADER = "DL"

        /**
         * 系统管理员
         */
        const val ADMIN = "AD"
    }
}